package com.zhuss.core.processor;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.exceptions.TooManyResultsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.CreateScheduleBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.ScheduleDao;
import com.zhuss.core.db.entity.Schedule;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.Area;
import com.zhuss.enums.FoodTime;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.util.DateUtil;
import com.zhuss.util.JsonUtil;

/**
 * @Author: zhushanshuo
 * @Date: 2020/03/05 20:20
 */
@Component("scheduleCreate")
public class ScheduleCreate extends AbstractProcessor<Object, CreateScheduleBo> implements Processor<Object, CreateScheduleBo> {
	
	private static final String startStr = "16:25";
	
	private static final String endStr = "17:55";
	
	private static final Integer minuteNum = 5;
	
	private static final String zbStart = "11:00";
	
	private static final String zbEnd = "13:00";
	
	private static final String middle = "12:00";
	
	private static final Integer 星期一 = 2;
	
	private static final Integer 星期三 = 4;
	
	private static final Integer 星期四 = 5;
	
	
	
    @Autowired
    private ScheduleDao scheduleDao;

    @Override
    public Object process(CreateScheduleBo bo) {
    	// 初始化 bo
    	initBo(bo);
    	
    	List<Date> dateList = bo.getList();
    	
    	// 西区点炒时间区间限制单数
    	for (Date time : dateList) {
    		Schedule record = new Schedule();
    		record.setDate(DateUtil.toDateOnly(new Date()));
    		record.setTime(time);
    		
    		Calendar cal = Calendar.getInstance();
    		cal.setTime(time);
    		cal.add(Calendar.MINUTE, 5);
    		record.setEnd(cal.getTime());
    		record.setTotal(3);
    		record.setUsed(0);
    		record.setArea(Area.西区.key());
    		
    		record.setCreator("系统");
    		record.setCreatorId(1l);
    		record.setCreateTime(new Date());
    		
    		Schedule entity = scheduleDao.getByTime(time, record.getEnd(), Area.西区.key());
    		if (entity == null) {
    			scheduleDao.create(record);
    		}
    		
    	}
    	
    	// 东区点炒晚餐预定限制单数
    	Schedule record = new Schedule();
		record.setDate(DateUtil.toDateOnly(new Date()));
		
		Date time = DateUtil.toDateByTimeMinute(startStr);
		
		record.setTime(time);
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);
		cal.add(Calendar.MINUTE, 90 + 5);
		record.setEnd(cal.getTime());
		record.setTotal(1000);
		record.setUsed(0);
		record.setArea(Area.东区.key());
		
		record.setCreator("系统");
		record.setCreatorId(1l);
		record.setCreateTime(new Date());
		
		Schedule entity = scheduleDao.getByTime(time, record.getEnd(), Area.东区.key());
		if (entity == null) {
			scheduleDao.create(record);
		}
		
		// 中区点炒晚餐预定限制单数
		Date today = DateUtil.toDateOnly(new Date());
		cal = Calendar.getInstance();
		cal.setTime(today);
		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(today);
		for (int i = 1; i <= 31; i ++) {
			record = new Schedule();
			record.setDate(DateUtil.toDateOnly(cal2.getTime()));
			cal2.add(Calendar.DAY_OF_YEAR, i);
			
			time = DateUtil.toDateByTimeMinute(zbStart);
			Date end = DateUtil.toDateByTimeMinute(zbEnd);
			
			cal.setTime(time);
			cal.add(Calendar.DAY_OF_YEAR, i - 1);
			time = cal.getTime();
			
			cal.setTime(end);
			cal.add(Calendar.DAY_OF_YEAR, i - 1);
			end = cal.getTime();
			
			record.setTime(time);
			
			record.setEnd(end);
			record.setTotal(1000);
			record.setUsed(0);
			record.setArea(Area.中区.key());
			
			record.setCreator("系统");
			record.setCreatorId(1l);
			record.setCreateTime(new Date());
			
			entity = scheduleDao.getByTime(time, record.getEnd(), Area.中区.key());
			if (entity == null) {
				scheduleDao.create(record);
			}
		}
		
		
		// 东区的公务接待限制单数
		today = DateUtil.toDateOnly(new Date());
		cal = Calendar.getInstance();
		cal.setTime(today);
		for (int i = 1; i <= 31; i ++) {
			
			cal.add(Calendar.DAY_OF_YEAR, 1);
			
			record = new Schedule();
			record.setDate(cal.getTime());
			record.setFoodTime(FoodTime.午餐.key());
			
			record.setTotal(4);
			record.setUsed(0);
			record.setArea(Area.东区.key());
			
			record.setCreator("系统");
			record.setCreatorId(1l);
			record.setCreateTime(new Date());
			
			try {
				
				entity = scheduleDao.getByFoodTime(record.getDate(), record.getFoodTime(), Area.东区.key());
				if (entity == null) {
					scheduleDao.create(record);
				}
				
			} catch (TooManyResultsException e) {
				e.printStackTrace();
			}
			record.setScheduleId(null);
			record.setArea(Area.东区.key());
			record.setFoodTime(FoodTime.晚餐.key());
			entity = scheduleDao.getByFoodTime(record.getDate(), record.getFoodTime(), Area.东区.key());
			if (entity == null) {
				scheduleDao.create(record);
			}
			
			
			record.setScheduleId(null);
			record.setArea(Area.西区.key());
			record.setFoodTime(FoodTime.午餐.key());
			entity = scheduleDao.getByFoodTime(record.getDate(), record.getFoodTime(), Area.西区.key());
			if (entity == null) {
				scheduleDao.create(record);
			}
			
			record.setScheduleId(null);
			record.setArea(Area.西区.key());
			record.setFoodTime(FoodTime.晚餐.key());
			entity = scheduleDao.getByFoodTime(record.getDate(), record.getFoodTime(), Area.西区.key());
			if (entity == null) {
				scheduleDao.create(record);
			}
			
		}
        return null;
    }
    
    private void initBo(CreateScheduleBo bo) {
		List<Date> dateList = DateUtil.toDateListByTimeMinute(startStr, endStr, minuteNum);
		bo.setList(dateList);
	}

	@Override
	public Object log(CreateScheduleBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.定时任务.key());
    	record.setTargetId(null);
    	record.setTargetName(JsonUtil.toJson(param.getList()));
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(null);
    	
    	record.setCreatorId(null);
    	record.setCreator(null);
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}

	@Override
	public void check(CreateScheduleBo param) throws WpsException {
		// TODO Auto-generated method stub
		
	}
	
	public static List<Date> getStart2(Date date) throws ParseException {
		String dateStr = DateUtil.sdfDate.format(date);
		Date start = DateUtil.sdfTime.parse(dateStr + zbStart);
		
		Date l = DateUtil.sdfTime.parse(dateStr + middle);
		//Date start = DateUtil.toDateByTimeMinute(zbStart);
		Calendar cal = Calendar.getInstance();
		cal.setTime(start);
		Integer dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
		Date end = new Date();
		if ((dayOfWeek >= 星期一 && dayOfWeek < 星期三) // 礼拜一到礼拜三(不包含礼拜三)
				// || (dayOfWeek == 星期一 && l.getTime() < date.getTime())  // 礼拜一 12:00之后 
				|| (dayOfWeek == 星期三 && l.getTime() > date.getTime())  // 礼拜三 12:00之前
				
				) {
			cal.set(Calendar.DAY_OF_WEEK, 星期四);
			start = cal.getTime();
			cal.add(Calendar.HOUR_OF_DAY, 2);
			end = cal.getTime();
		}
		
		if ((dayOfWeek > 星期三 && dayOfWeek < 8) || dayOfWeek == 1   // 礼拜三(不包含礼拜三)到礼拜一   还有礼拜天
				|| (dayOfWeek == 星期三 && l.getTime() < date.getTime())  // 礼拜三 12:00之后
				// || (dayOfWeek == 星期一 && l.getTime() > date.getTime())  // 礼拜一 12:00之前 
				
				) {
			cal.set(Calendar.DAY_OF_WEEK, 星期四);
			if (dayOfWeek >= 星期三 && dayOfWeek < 8) {
				cal.add(Calendar.WEEK_OF_YEAR, 1);
			}
			start = cal.getTime();
			cal.add(Calendar.HOUR_OF_DAY, 2);
			end = cal.getTime();
		}
		List<Date> list = new ArrayList<Date>();
		list.add(start);
		list.add(end);
		return list;
	}

}
